package com.lyc.service.impl;

import com.lyc.dao.ColorMapper;
import com.lyc.dao.ProductMapper;
import com.lyc.dao.SizeMapper;
import com.lyc.entity.Color;
import com.lyc.entity.Product;
import com.lyc.entity.Size;
import com.lyc.entity.vo.PageBean;
import com.lyc.service.ISizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SizeServiceImpl implements ISizeService {
    
    @Autowired
    private SizeMapper sizeMapper;

    @Autowired
    private ColorMapper colorMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageBean<Size> selectByPage(Integer currentPage, int sizePageSize) {
        PageBean<Size> page = new PageBean<>();
        page.setCurrentPage(currentPage);
        page.setPageSize(sizePageSize);
        page.setTotalCount(sizeMapper.selectAllCount());
        page.setData(sizeMapper.selectByPage(page.getStart(),page.getPageSize()));
        page.setTotalPage((int)Math.ceil((double)(page.getTotalCount())/page.getPageSize()));
        return page;
    }

    @Override
    public Size selectBySizeId(Integer sizeId) {
        return sizeMapper.selectByPrimaryKey(sizeId);
    }

    @Override
    public void addSize(Size size) {
        sizeMapper.insertSelective(size);
    }

    @Override
    public void updateSize(Size size) {
        sizeMapper.updateByPrimaryKeySelective(size);
    }

    @Override
    public void deleteSizeBySizeId(Integer sizeId) {
        sizeMapper.deleteByPrimaryKey(sizeId);
    }

    @Override
    public List<Size> getAll() {
        return sizeMapper.selectAll();
    }

    @Override
    public void operateSize(List<Integer> ids, Integer pid) {
        //获取该商品的所有颜色
        Product product = productMapper.selectByPrimaryKey(pid);
        //删除该商品的所有尺码/颜色
        sizeMapper.deleteByPid(pid);
        ///添加该商品原有的颜色
        colorMapper.insertBatchByColors(product.getColors(),pid);
        //添加商品新增尺码
        sizeMapper.insertBatchByIds(ids,pid);
    }
}
